<template>
  <div id="bg">
      <img src="../assets/logo1.png" alt="logo"
           style ="height:150px; width:150px; float: left; margin: 0px 0 0px 0px;background: #f2fff8">
    <span  style="position:absolute;left:0;right:0;bottom:0;top: 80px;
    color: #fff48c;font-size: 50px;font-family:Cursive">批发市场管理系统</span>
    <br>
    <span  style="position:absolute;left:0;right:0;bottom:0;top: 150px;
    color: #fff48c;font-size: 45px;font-family:Cursive">Market System</span>

    <div id="login"  >
      <img id="img2" src="https://pic.baike.soso.com/p/20140328/20140328172111-1402278836.jpg">
      <div class="login_input">
        <p>
        账号:<input type="text" v-model="userObj.username" placeholder="请输入账号"><br>
        密码：<input type="text" v-model="userObj.password" placeholder="请输入密码"><br>

          <select v-model="selected" >
            <option v-bind:value="selected">请选择身份</option>
            <option>顾客</option>
            <option>店铺主</option>
            <option>管理员</option>
          </select><br>
        </p>>
      <a-button @click="login" style="background-color: rgba(25,110,200,0.8);width: 200px;color: white" >登录</a-button>
      </div>
    </div>
  </div>
</template>

<script>
// eslint-disable-next-line no-unused-vars
import axios from 'axios'
export default {
  name: 'login',
  data () {
    return {
      userObj:{
        username:'',
        password:''
      },
      selected:"",
    }
  },

  methods: {
    login () {
      let _this = this
      if (this.userObj.username===''||this.userObj.password==='') {
        alert('账号或密码不能为空')
      } else {
        axios({
          method: 'post',
          url: this.baseURL + 'login.ctl',
         data : _this.userObj
        }).then(res => {
          this.userObj = {
            username: _this.userObj.username,
            password: _this.userObj.password
          }
          console.log(res.data)
          //  _this.userToken = res.data.token
          // // 将用户token保存到vuex中
          //  _this.changeLogin({ Authorization: _this.userToken })
          //  console.log(res.data)
          if (res.data.message === '用户名或密码不匹配') {
            alert('用户名或密码不匹配')
          } else if (res.data.message === '数据库操作异常') {
            alert('数据库操作异常')
          } else if (res.data.message === '网络异常') {
            alert('网络异常')
          } else {
              let item = res.data
              if (this.selected === item.roles && this.selected === '顾客'){
                _this.$router.push('indexOfCustomer')
                window.localStorage.setItem('userName', res.data.actor.name)
                return
              } else if (this.selected === item.roles && this.selected === '店铺主'){
                _this.$router.push('indexOfShopOwner')
                window.localStorage.setItem('userName', res.data.actor.name)
              } else if (this.selected === item.roles && this.selected === '管理员'){
                _this.$router.push('/indexOfAdministrator')
                window.localStorage.setItem('userName', res.data.actor.name)
                return
              } else {
                alert('身份不匹配')
              }
          }
        }).catch(error => {
          alert('账号或密码错误')
          console.log(error)
        })
      }
    }
  }
}
</script>

<style scoped>
#bg{width:100% ;
  height: 750px;
  background-image:url("../assets/loginbg.jpg");
  background-attachment: fixed;
}
#img2{
  width:380px;
  height:280px;
  margin-left: -250px;
  margin-top: 70px;
}
 #login{
   width: 700px;
   height: 400px;
   position:absolute;
   left:0;
   top:150px;
   right:0;
   bottom:0;
   margin:auto;
   z-index: 100;
   background-image: url("https://tse2-mm.cn.bing.net/th/id/OIP.ACXNz9vBVAU7rHzIzQI4GwHaE1?pid=Api&rs=1");
   box-shadow:0 0 30px rgba(255, 82, 91, 0.4);
   text-align: center;
 }
 .login_input{
   margin-left: 450px;
   margin-top: -300px;
 }
 #login input{
   margin-bottom:40px;
   line-height: 25px;
   width:180px;
 }
 #login input:focus{
   box-shadow:0 0 10px rgba(0, 0, 0, 0.4);
 }
 #login select{
   line-height: 25px;
   width:180px;
   height:28px
 }
 #login button{
   width:180px;
   border-radius: 5px;
   margin-top: 30px;
   box-shadow:0 0 5px rgba(0, 0, 0, 0.4);
 }
</style>
